package com.base.logistics.config;

import com.base.logistics.jdl.request.JdlApiRequestConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自动配置.
 * @since 2021-03-03
 */
@Configuration
@ConditionalOnClass(JdlApiRequestConfig.class)
@EnableConfigurationProperties(JdlExpressProperties.class)
@ConditionalOnProperty(prefix = "logistics.express.jdl", value = "enabled", havingValue = "true", matchIfMissing = true)
public class JdlExpressAutoConfiguration {

    private final JdlExpressProperties properties;

    public JdlExpressAutoConfiguration(JdlExpressProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean(JdlApiRequestConfig.class)
    public JdlApiRequestConfig jdlApiRequestConfig() throws Exception {
        JdlConfig config = new JdlConfig();
        config.setEnabled(properties.getEnabled());
        config.setPro(properties.getPro());
        config.setAppKey(properties.getAppKey());
        config.setAppSecret(properties.getAppSecret());
        config.setRefreshToken(properties.getRefreshToken());
        config.setCustomerCode(properties.getCustomerCode());
        config.setPrintTempUrl(properties.getPrintTempUrl());
        config.setPrintOffsetLeft(properties.getPrintOffsetLeft());
        config.setPrintOffsetTop(properties.getPrintOffsetTop());
        JdlApiRequestConfig service = new JdlApiRequestConfig();
        service.setConfig(config);
        return service;
    }
}
